﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Collections;
using ShareB.Providers;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;


namespace ShareB.Models
{

    #region Models

    
    public class RelationsModel
    {
        [Required]
        [DisplayName("UserID")]
        public int UserID { get; set; }

        // eu sigo
        [DisplayName("Publishers")]
        public List<PublisherModel> Publishers { get; set; }

        // quem me segue
        [DisplayName("Followers")]
        public List<FollowerModel> Followers { get; set; }

        public RelationsModel()
        {
            Publishers = new List<PublisherModel>();
            Followers = new List<FollowerModel>();
        }
    }

    // QUEM ME SEGUE
    public class FollowerModel
    {
        [Required]
        [DisplayName("UserID")]
        public int UserID { get; set; }

        [Required]
        [DisplayName("User name")]
        public string Username { get; set; }

 
        [DisplayName("Photograph")]
        public byte[] Photograph { get; set; }

    }

    //EU SIGO ESTAS PESSOAS
    public class PublisherModel
    {
        [Required]
        [DisplayName("UserID")]
        public int UserID { get; set; }

        [Required]
        [DisplayName("User name")]
        public string Username { get; set; }

 
        [DisplayName("Photograph")]
        public byte[] Photograph { get; set; }
    }

    #endregion

    #region Services

    public class FollowerService
    {
        ShareBDataContext db;

        public FollowerService()
        {
            db = new ShareBDataContext();
        }

        //Implements Load Followers
        public RelationsModel LoadFollowers(int userID)
        {
            RelationsModel model = new RelationsModel();
            model.UserID = userID;

            foreach (Follower f in db.Followers)
            {
                // Adicionar quem me segue
                if (f.BroadcasterID == userID)
                {
                    FollowerModel fol = new FollowerModel();
                    fol.UserID = db.Users.Single(fo => fo.UserID == f.FollowerID).UserID;
                    fol.Username = db.Users.Single(fo => fo.UserID == f.FollowerID).Username;
                    //fol.Photograph = db.Users.Single(fo => fo.UserID == f.FollowerID).Foto;
                    model.Followers.Add(fol);
                }
                // Adicionar quem eu sigo
                if (f.FollowerID == userID)
                {
                    PublisherModel pub = new PublisherModel();
                    pub.UserID = db.Users.Single(p => p.UserID == f.BroadcasterID).UserID;
                    pub.Username = db.Users.Single(p => p.UserID == f.BroadcasterID).Username;
                    //pub.Photograph = db.Users.Single(p => p.UserID == f.BroadcasterID).Foto;
                    model.Publishers.Add(pub);
                }
            }
            
            return model;
        }

        //Add publishers - view and add publishers
        public bool AddPublishers(int myuser, int id)
        {
            if (myuser != id)
            {
                Follower f = new Follower { FollowerID = myuser, BroadcasterID = id };
                db.Followers.InsertOnSubmit(f);
                db.SubmitChanges();
                return true;
            }
            else
                return false;
        }

        //Remove publishers - view and remove followers
        public bool RemovePublishers(int id, int myuser)
        {
            foreach (Follower f1 in db.Followers)
            {
                if (f1.FollowerID == myuser && f1.BroadcasterID == id)
                {
                    db.Followers.DeleteOnSubmit(f1);
                    db.SubmitChanges();
                    return true;
                }
            }
            return false;
        }

        //Get new followers - view news followers
        public IList<PublisherModel> SearchPublishers(string searchValue)
        {
            IList<PublisherModel> temp = new List<PublisherModel>();
            foreach (User u in db.Users)
            {
                if (u.Username.ToLower().Contains(searchValue.ToLower())) 
                {
                    temp.Add(new PublisherModel { UserID = u.UserID, Username = u.Username });
                }
                
            }
            return temp;
        }

        //Implements View Publishers
        public IList<PublisherModel> ViewPublishers(int userID)
        {
            RelationsModel model = new RelationsModel();
            model.UserID = userID;

            foreach (Follower f in db.Followers)
            {
                // Quem eu sigo
                if (f.FollowerID == userID)
                {
                    PublisherModel pub = new PublisherModel();
                    pub.UserID = db.Users.Single(p => p.UserID == f.BroadcasterID).UserID;
                    pub.Username = db.Users.Single(p => p.UserID == f.BroadcasterID).Username;
                    //pub.Photograph = db.Users.Single(p => p.UserID == f.BroadcasterID).Foto;
                    model.Publishers.Add(pub);
                }
            }

            return model.Publishers;
        }

    }

    #endregion

    #region Validators

    #endregion

}
